package com.burstly.lib.network.beans.cookie;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.burstly.lib.util.LoggerExt;
import com.burstly.lib.util.Utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public final class SQLiteCookieStorage
  implements ICookieStorage
{
  private static final LoggerExt LOG = LoggerExt.getInstance();
  private static final String TAG = "SQLiteCookieStorage";
  private CookiePersistanceManager mCookieDatabase;
  private volatile boolean mUsableState;

  public SQLiteCookieStorage(Context paramContext)
  {
    try
    {
      this.mCookieDatabase = new CookiePersistanceManager(paramContext);
      this.mUsableState = true;
      LOG.logDebug("SQLiteCookieStorage", "Initialization completed.", new Object[0]);
      return;
    }
    catch (Throwable localThrowable)
    {
      LOG.logWarning("SQLiteCookieStorage", "Exception during initialization sql database.", new Object[0]);
      LOG.logThrowable("SQLiteCookieStorage", localThrowable);
      this.mCookieDatabase = null;
    }
  }

  public void clear()
  {
    if (this.mUsableState)
    {
      this.mCookieDatabase.beginTransaction();
      this.mCookieDatabase.deleteAll();
      this.mCookieDatabase.commit();
    }
  }

  public void deleteCookie(String paramString)
  {
    if ((paramString != null) && (this.mUsableState))
    {
      this.mCookieDatabase.beginTransaction();
      this.mCookieDatabase.deleteCookie(paramString);
      this.mCookieDatabase.commit();
    }
  }

  public Collection<CookieHolderWrapper> getValidCookies()
  {
    Object localObject;
    if (!this.mUsableState)
      localObject = Collections.emptyList();
    do
    {
      return localObject;
      List localList = this.mCookieDatabase.getSavedCookies();
      if ((localList == null) || (localList.isEmpty()))
        break;
      localObject = new ArrayList();
      long l1 = System.currentTimeMillis();
      this.mCookieDatabase.beginTransaction();
      Iterator localIterator = localList.iterator();
      while (localIterator.hasNext())
      {
        CookieTO localCookieTO = (CookieTO)localIterator.next();
        long l2 = localCookieTO.getExpirationTime().longValue();
        if (l2 < l1)
        {
          this.mCookieDatabase.deleteCookie(localCookieTO.getName());
          LoggerExt localLoggerExt = LOG;
          Object[] arrayOfObject = new Object[2];
          arrayOfObject[0] = localCookieTO;
          arrayOfObject[1] = new Date(l2);
          localLoggerExt.logDebug("SQLiteCookieStorage", "Cookie expired: {0}. Expiration time: {1}", arrayOfObject);
        }
        else
        {
          ((List)localObject).add(new CookieHolderWrapper((CookieHolder)Utils.fromJson(localCookieTO.getCookieContent(), CookieHolder.class), localCookieTO.getExpirationTime().longValue()));
        }
      }
      this.mCookieDatabase.commit();
    }
    while (((List)localObject).size() > 0);
    return Collections.emptyList();
  }

  public void processCookies(Collection<CookieHolder> paramCollection)
    throws Exception
  {
    if (paramCollection == null)
      throw new IllegalArgumentException("Cannot process null-referenced list of cookies.");
    if ((!this.mUsableState) || (paramCollection.isEmpty()))
      return;
    long l1 = System.currentTimeMillis();
    this.mCookieDatabase.beginTransaction();
    Iterator localIterator = paramCollection.iterator();
    while (localIterator.hasNext())
    {
      CookieHolder localCookieHolder = (CookieHolder)localIterator.next();
      if (localCookieHolder != null)
      {
        boolean bool1 = this.mCookieDatabase.exists(localCookieHolder.getName());
        long l2 = l1 + 1000L * localCookieHolder.getMaxage().intValue();
        CookiePersistanceManager localCookiePersistanceManager = this.mCookieDatabase;
        if (!bool1);
        for (boolean bool2 = true; ; bool2 = false)
        {
          localCookiePersistanceManager.saveOrUpdateCookie(localCookieHolder, bool2, l2);
          if (!bool1)
            break label189;
          LoggerExt localLoggerExt2 = LOG;
          Object[] arrayOfObject2 = new Object[2];
          arrayOfObject2[0] = localCookieHolder;
          arrayOfObject2[1] = new Date(l2);
          localLoggerExt2.logDebug("SQLiteCookieStorage", "Cookie updated: {0}. Expiration time: {1}", arrayOfObject2);
          break;
        }
        label189: LoggerExt localLoggerExt1 = LOG;
        Object[] arrayOfObject1 = new Object[2];
        arrayOfObject1[0] = localCookieHolder;
        arrayOfObject1[1] = new Date(l2);
        localLoggerExt1.logDebug("SQLiteCookieStorage", "New cookie added: {0}. Expiration time: {1}", arrayOfObject1);
      }
    }
    this.mCookieDatabase.commit();
  }

  private static class CookiePersistanceManager extends SQLiteOpenHelper
  {
    private static final String COOKIE_DB_NAME = "cookiedb";
    private static final String COOKIE_TABLE_CREATE = "CREATE TABLE cookie (name  TEXT PRIMARY KEY, cookie_content TEXT, expires INTEGER);";
    private static final int DATABASE_VERSION = 2;
    private static final String EXIST_COOKIE_SQL = "SELECT name FROM cookie WHERE name=?";
    static final String TABLE_NAME = "cookie";
    final SQLiteDatabase mWritableDatabase = getWritableDatabase();

    CookiePersistanceManager(Context paramContext)
    {
      super("cookiedb", null, 2);
    }

    void beginTransaction()
    {
      this.mWritableDatabase.beginTransaction();
    }

    void commit()
    {
      this.mWritableDatabase.setTransactionSuccessful();
      this.mWritableDatabase.endTransaction();
    }

    void deleteAll()
    {
      SQLiteCookieStorage.LOG.logDebug("SQLiteCookieStorage", "Deleting all cookies from DB...", new Object[0]);
      this.mWritableDatabase.delete("cookie", null, null);
      SQLiteCookieStorage.LOG.logDebug("SQLiteCookieStorage", "All cookies are deleted.", new Object[0]);
    }

    void deleteCookie(String paramString)
    {
      SQLiteCookieStorage.LOG.logDebug("SQLiteCookieStorage", "Deleting ''{0}'' cookie from DB...", new Object[] { paramString });
      this.mWritableDatabase.delete("cookie", "name=?", new String[] { paramString });
      SQLiteCookieStorage.LOG.logDebug("SQLiteCookieStorage", "Cookie ''{0}'' deleted.", new Object[] { paramString });
    }

    boolean exists(String paramString)
    {
      Cursor localCursor = this.mWritableDatabase.rawQuery("SELECT name FROM cookie WHERE name=?", new String[] { paramString });
      boolean bool = false;
      if (localCursor != null)
      {
        bool = localCursor.moveToFirst();
        if (!localCursor.isClosed())
          localCursor.close();
      }
      return bool;
    }

    List<CookieTO> getSavedCookies()
    {
      ArrayList localArrayList = new ArrayList();
      SQLiteCookieStorage.LOG.logDebug("SQLiteCookieStorage", "Loading cookies from DB...", new Object[0]);
      Cursor localCursor = this.mWritableDatabase.rawQuery("SELECT name, cookie_content, expires FROM cookie", null);
      if (localCursor != null)
      {
        if (localCursor.moveToFirst())
        {
          do
            localArrayList.add(new CookieTO(localCursor.getString(0), localCursor.getString(1), Long.valueOf(localCursor.getLong(2))));
          while (localCursor.moveToNext());
          SQLiteCookieStorage.LOG.logDebug("SQLiteCookieStorage", "Cookies were successfully loaded from DB.", new Object[0]);
        }
        if (!localCursor.isClosed())
          localCursor.close();
      }
      if (localArrayList.isEmpty())
        SQLiteCookieStorage.LOG.logDebug("SQLiteCookieStorage", "No saved cookies in DB.", new Object[0]);
      return localArrayList;
    }

    public void onCreate(SQLiteDatabase paramSQLiteDatabase)
    {
      paramSQLiteDatabase.execSQL("CREATE TABLE cookie (name  TEXT PRIMARY KEY, cookie_content TEXT, expires INTEGER);");
    }

    public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2)
    {
      LoggerExt localLoggerExt = SQLiteCookieStorage.LOG;
      Object[] arrayOfObject = new Object[2];
      arrayOfObject[0] = Integer.valueOf(paramInt1);
      arrayOfObject[1] = Integer.valueOf(paramInt2);
      localLoggerExt.logWarning("SQLiteCookieStorage", "Upgrading database from version {0} to {1}, which will destroy all old data", arrayOfObject);
      paramSQLiteDatabase.execSQL("DROP TABLE IF EXISTS cookie");
      onCreate(paramSQLiteDatabase);
    }

    void saveOrUpdateCookie(CookieHolder paramCookieHolder, boolean paramBoolean, long paramLong)
    {
      String str1 = Utils.toJson(paramCookieHolder);
      String str2 = paramCookieHolder.getName();
      ContentValues localContentValues = new ContentValues();
      localContentValues.put("name", str2);
      localContentValues.put("cookie_content", str1);
      localContentValues.put("expires", Long.valueOf(paramLong));
      if (!paramBoolean)
      {
        SQLiteCookieStorage.LOG.logDebug("SQLiteCookieStorage", "Updating cookie {0}", new Object[] { paramCookieHolder });
        this.mWritableDatabase.update("cookie", localContentValues, "name=?", new String[] { str2 });
        return;
      }
      SQLiteCookieStorage.LOG.logDebug("SQLiteCookieStorage", "Saving cookie {0}", new Object[] { paramCookieHolder });
      this.mWritableDatabase.insert("cookie", null, localContentValues);
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.network.beans.cookie.SQLiteCookieStorage
 * JD-Core Version:    0.6.2
 */